#!/bin/bash

echo "🚀 开始部署云监控系统..."

# 检查Docker是否安装
if ! command -v docker &> /dev/null; then
    echo "❌ Docker未安装，请先安装Docker"
    echo "📚 安装参考: https://docs.docker.com/engine/install/"
    exit 1
fi

# 检查Docker Compose是否安装
if ! command -v docker-compose &> /dev/null; then
    echo "❌ Docker Compose未安装，请先安装Docker Compose"
    echo "📚 安装参考: https://docs.docker.com/compose/install/"
    exit 1
fi

echo "✅ Docker环境检查通过"

# 创建必要的目录
echo "📁 创建目录结构..."
mkdir -p prometheus grafana

# 检查配置文件是否存在
if [ ! -f "prometheus/prometheus.yml" ]; then
    echo "❌ 缺少 prometheus/prometheus.yml 配置文件"
    exit 1
fi

if [ ! -f "grafana/custom.ini" ]; then
    echo "❌ 缺少 grafana/custom.ini 配置文件"
    exit 1
fi

echo "✅ 配置文件检查通过"

# 停止现有服务（如果有）
echo "🛑 停止现有服务..."
docker-compose down

# 启动服务
echo "🐳 启动Docker容器..."
docker-compose up -d

# 等待服务启动
echo "⏳ 等待服务启动(30秒)..."
sleep 30

# 检查服务状态
echo "🔍 检查服务状态..."
docker-compose ps

echo ""
echo "🎉 部署完成！"
echo "========================"
echo "📊 访问地址："
echo "   - Grafana面板: http://$(curl -s ifconfig.me):3000"
echo "     用户名: admin"
echo "     密码: admin123"
echo "   - Prometheus: http://$(curl -s ifconfig.me):9090"
echo "   - Node Exporter: http://$(curl -s ifconfig.me):9100"
echo ""
echo "🔧 常用命令："
echo "   - 查看日志: docker-compose logs"
echo "   - 停止服务: docker-compose down"
echo "   - 重启服务: docker-compose restart"
echo "========================"
